package _6subset;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Gong Cheng
 * @date 2021/1/19 1:51 下午
 **/
public class permute46 {

    /***
     * 搜索回溯
     */
    public List<List<Integer>> permute(int[] nums){
        List<List<Integer>> res = new ArrayList<>();

        List<Integer> output = new ArrayList<>();
        for (int num : nums){
            output.add(num);
        }

        int n = nums.length;
        backtrack(n, output, res, 0);
        return res;
    }

    public void backtrack(int n, List<Integer> output, List<List<Integer>> res, int first){
        // 所有数都填完
        if (first == n){
            res.add(new ArrayList<Integer>(output));
        }
        for(int i = first; i < n; i++){
            // 动态维护数组
            Collections.swap(output,first, i);
            //继续递归天下一个数
            backtrack(n, output, res, first + 1);
            // 撤销操作
            Collections.swap(output, first, i);
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[] {1,2,3};
        System.out.println(new permute46().permute(nums));

    }
}
